<!-- 友链管理 -->
<template>
  <el-button type="primary" @click="add('add')" style="margin-bottom: 10px;">新增</el-button>
  <el-table :data="data.tableData" row-key="id" table-layout="auto" style="width: 100%">
    <el-table-column prop="name" label="友链名称" width="" />
    <el-table-column prop="url" label="友链地址" width="" />
    <ma-dict-column prop="linkFlag" label="友链类型" dict-type="link_type"></ma-dict-column>
    <el-table-column prop="imageUrl" label="图片" width="">
      <template #default="scope">
        <img :src="scope.row.imageUrl" alt="" style="width: 50px;height: 50px;">
      </template>
    </el-table-column>
    <el-table-column prop="status" label="启用状态" width="">
      <template #default="scope">
        <el-switch v-model="scope.row.status" inline-prompt @change="switchChange(scope.row)" :active-value="1" :inactive-value="0" active-text="启动" inactive-text="禁用" />
      </template>
    </el-table-column>
    <el-table-column prop="createTime" label="创建时间" width="" />
    <el-table-column prop="updateTime" label="更新时间" width="" />
    <el-table-column prop="sort" label="排序" width="" />
    <el-table-column prop="" label="操作" width="" fixed="right">
      <template #default="scope">
        <el-button type="primary" @click="add('edit',scope.row)">编辑</el-button>
        <el-button type="danger" @click="deleteColumn(scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[10, 20, 30, 50]" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="getList" @current-change="getList" style="margin-top: 20px;display:flex;justify-content: flex-end;" />
  <dialogForm ref="dialogFormRef" @resetList="getList"></dialogForm>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, nextTick } from 'vue'
import { listLink, editLink, deleteLink } from '@/api/allApi'
import dialogForm from './dialogForm.vue'
import { ElMessage, ElMessageBox } from 'element-plus/es'

// let tableData = reactive([])
const pageNum = ref(1) //第几页
const pageSize = ref(10) //每页多少条
const total = ref(0) //总条数
const data = reactive({
	tableData: []
})

onMounted(() => {
	getList()
})

const getList = () => {
	listLink({
		websiteId: Number(localStorage.getItem('WebsiteId')), //网站id 1：鞍钢官网 2：博物馆
		page: pageNum.value,
		size: pageSize.value
	}).then((res: any) => {
		if (res.code === 0) {
			data.tableData = res.data.records
			total.value = res.data.total
		} else {
			ElMessage.success(res.msg)
		}
	})
}
const switchChange = (row: any) => {
	editLink({
        id: row.id,
		status: row.status
    }).then(res => {
		if (res.code === 0) {
			ElMessage.success('操作成功')
			getList()
		} else {
		}
	})
}
const dialogFormRef = ref()
const add = (type: string, row: any) => {
	dialogFormRef.value.init(type, row)
}
const deleteColumn = (row: any) => {
	ElMessageBox.confirm(`确定删除这个友链吗？`, '警告', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	}).then(() => {
		deleteLink(row.id).then((res: any) => {
			if (res.code === 0) {
				ElMessage.success('删除成功')
				getList()
			} else {
				ElMessage.error(res.msg)
			}
		})
	})
}
</script>

<style lang="scss" scoped>
//搭配el-table里的table-layout="auto"属性，再让每一列里的width为空即可展现出适应表格内容的列宽
:deep(.el-table .cell) {
	width: max-content;
	display: inline-block;
}
</style>
